package com.ac.scum.log.web.model.wechat.mp.task;

import com.ac.scum.wechat.bean.WechatMpKeyword;
import com.ac.scum.log.web.model.wechat.mp.mapper.WechatMpKeywordMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jakarta.annotation.PostConstruct;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@AllArgsConstructor
public class CacheTask {
    private final RedisTemplate<String, Object> redisTemplate;
    private final WechatMpKeywordMapper wechatMpKeywordMapper;

    @PostConstruct
    @Scheduled(timeUnit = TimeUnit.HOURS, initialDelay = 1, fixedDelay = 1)
    void loadWechatKeywordCache() {
        String key = "wechat::mp::keyword";
        List<WechatMpKeyword> wechatMpKeywords = wechatMpKeywordMapper.selectList(Wrappers.emptyWrapper());
        redisTemplate.delete(key);
        wechatMpKeywords.forEach(item -> redisTemplate.opsForList().rightPush(key, item));
    }
}
